package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 * 后台接口
 *
 * @author zr
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/cms/service/banner")
//@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * banner 分页查询
     *
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取Banner分页列表")
    @GetMapping("pageBanner/{current}/{limit}")
    public Result pageBanner(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit) {
        Page<CrmBanner> page = new Page<>(current, limit);
        crmBannerService.page(page, null);
        return Result.ok(page);
    }

    /**
     * 通过Id查询Banner
     *
     * @param bannerId
     * @return
     */
    @ApiOperation(value = "获取Banner")
    @GetMapping("getBanner/{bannerId}")
    public Result getBanner(@PathVariable String bannerId) {
        CrmBanner crmBanner = crmBannerService.getById(bannerId);
        return Result.ok(crmBanner);
    }

    /**
     * 添加Banner
     *
     * @param crmBanner
     * @return
     */
    @ApiOperation(value = "新增Banner")
    @PostMapping("addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner) {
        boolean flag = crmBannerService.save(crmBanner);
        return flag ? Result.ok().message("添加成功") : Result.fail().message("添加失败");
    }

    /**
     * 通过Id删除Banner
     *
     * @param bannerId
     * @return
     */
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("removeBanner/{bannerId}")
    public Result removeBanner(@PathVariable String bannerId) {
        boolean flag = crmBannerService.removeById(bannerId);
        return flag ? Result.ok().message("删除成功") : Result.fail().message("删除失败");
    }

    /**
     * 通过Id修改Banner
     *
     * @param crmBanner
     * @return
     */
    @PostMapping("updateBanner")
    public Result updateBanner(@RequestBody CrmBanner crmBanner) {
        boolean flag = crmBannerService.updateById(crmBanner);
        return flag ? Result.ok().message("修改成功") : Result.fail().message("修改失败");
    }
}

